<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="维度简称" prop="dimension">
        <el-input
          v-model="queryParams.dimension"
          placeholder="请输入维度简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dimension:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dimension:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dimension:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dimension:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dimensionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label=" 编号" align="center" prop="id" />
      <el-table-column label="简称" align="center" prop="dimension" />
      <el-table-column label="标题" align="center" prop="title" />
      <!-- <el-table-column label="描述" align="center" prop="describe" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dimension:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dimension:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改性格维度描述对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="简称" prop="dimension">
          <el-input
            v-model="form.dimension"
            placeholder="请输入维度简称"
            disabled
          />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入维度简称"
          />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" :limit="1" />
        </el-form-item>
        <el-form-item label="分享标题">
          <el-input
            v-model="form.shareTitle"
            placeholder="请输入分享标题"
          />
        </el-form-item>
        <el-form-item label="分享描述">
          <el-input
            v-model="form.shareDesc"
            placeholder="请输入分享描述"
          />
        </el-form-item>
        <el-form-item label="分享图片">
          <image-upload v-model="form.shareIcon" :limit="1" />
        </el-form-item>
        <el-form-item label="性格描述"></el-form-item>
        <el-row v-for="(item, index) in form.dimensionDescs" :key="index">
          性格标题：<el-input v-model="item.title" style="width: 300px"></el-input>
          <el-button
            style="margin-left: 10px;"
            type="danger"
            size="mini"
            v-if="form.dimensionDescs.length>1"
            @click.prevent="btnTitle(1, index)"
            >删除</el-button
          >
          <el-button
            style="margin-left: 10px;"
            type="success"
            size="mini"
            v-if="index == form.dimensionDescs.length - 1"
            @click.prevent="btnTitle(2, index)"
            >新增</el-button
          >
          <div style="margin: 10px;">
            <el-table :data="item.contents" style="width: 100%">
              <el-table-column label="内容">
                <template #default="scope">
                  <el-input
                    size="large"
                    v-model.trim="item.contents[scope.$index]"
                    placeholder="请输入内容"
                    style="width: 100%"
                  />
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button
                    type="danger"
                    size="mini"
                    plain
                    v-if="item.contents.length>1"
                    @click.prevent="btnContent(1, index, scope.$index)"
                    >删除</el-button
                  >
                  <el-button
                    type="success"
                    size="mini"
                    plain
                    v-if="scope.$index == item.contents.length - 1"
                    @click.prevent="btnContent(2, index, scope.$index)"
                    >新增</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-row>

        <!-- <el-form-item
          prop="dimensionDescs"
          label="标题"
          v-for="(item, index) in form.dimensionDescs"
          :key="index"
        >
          <el-input v-model="item.title" style="width: 300px"></el-input>
          <el-button
            type="danger"
            size="mini"
            @click.prevent="btnTitle(1, index)"
            >删除</el-button
          >
          <el-button
            type="success"
            size="mini"
            v-if="index == form.dimensionDescs.length - 1"
            @click.prevent="btnTitle(2, index)"
            >新增</el-button
          >
          <div v-for="(item2, index2) in item.contents" :key="index2">
            <el-input v-model="item.contents[index2]"></el-input>
            <el-button
              type="danger"
              size="mini"
              plain
              @click.prevent="btnContent(1, index, index2)"
              >删除</el-button
            >
            <el-button
              type="success"
              size="mini"
              plain
              v-if="index2 == item.contents.length - 1"
              @click.prevent="btnContent(2, index, index2)"
              >新增</el-button
            >
          </div>
        </el-form-item> -->
        <!-- <el-form-item
            v-for="(domain, index) in dynamicValidateForm.domains"
            :label="'域名' + index"
            :key="domain.key"
            :prop="'domains.' + index + '.value'"
            :rules="{
              required: true,
              message: '域名不能为空',
              trigger: 'blur',
            }"
          >
            <el-input v-model="domain.value"></el-input
            ><el-button @click.prevent="removeDomain(domain)">删除</el-button>
          </el-form-item> -->
        <!-- <el-form-item>
            <el-button @click="addDomain">新增域名</el-button>
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDimension,
  getDimension,
  delDimension,
  addDimension,
  updateDimension,
} from "@/api/system/dimension";

export default {
  name: "Dimension",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 性格维度描述表格数据
      dimensionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dimension: null,
        describe: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询性格维度描述列表 */
    getList() {
      this.loading = true;
      listDimension(this.queryParams).then((response) => {
        this.dimensionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dimension: null,
        describe: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加性格维度描述";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDimension(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改性格维度描述";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDimension(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDimension(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除性格维度描述编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDimension(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dimension/export",
        {
          ...this.queryParams,
        },
        `dimension_${new Date().getTime()}.xlsx`
      );
    },
    btnTitle(type, index) {
      if (type == 2) {
        this.form.dimensionDescs.push({
          title: "",
          contents: [""],
        });
      } else {
        this.form.dimensionDescs.splice(index, 1);
      }
    },
    btnContent(type, index, index2) {
      if (type == 2) {
        this.form.dimensionDescs[index].contents.push("");
      } else {
        this.form.dimensionDescs[index].contents.splice(index2, 1);
      }
    },
  },
};
</script>
